package com.person.leetcode.editor.cn;

/**
  * 42.接雨水
  * 2022-11-01 11:21:56
**/
public class Sequence_0042_TrappingRainWater {
    public static void main(String[] args) {
        Solution solution = new Sequence_0042_TrappingRainWater().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int trap(int[] height) {
        int ans = 0;
        int leftMax = height[0];//保存嘴边最大的
        int rightMax = height[height.length-1];//保存右边最大的
        int lef =0,rig = height.length-1;//记录当前所处的位置

        while (lef<rig){
            leftMax = Math.max(leftMax, height[lef]);
            rightMax = Math.max(rightMax, height[rig]);
            if (leftMax < rightMax){
                ans += leftMax - height[lef];
                lef ++;
            }else{
                ans += rightMax - height[rig];
                rig --;
            }
        }
        return ans;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}